package de.s9mtdev.lol_destiny;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class FlashView extends ImageView implements OnTouchListener{
	
	Long when = (long)0;
	String id;
	Paint paint = new Paint();
	Paint textpaint = new Paint();
	InSession insession;
	
	public FlashView(Context context) {
		super(context);		
		init();
	}
	
	public FlashView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public FlashView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {
		setOnTouchListener(this);
		paint.setColor(Color.YELLOW);
		paint.setStyle(Style.FILL);
		textpaint.setColor(Color.BLACK);
		textpaint.setAntiAlias(true);
		textpaint.setTextSize(14);
		id = this.getTag().toString();	
		insession = (InSession)getContext();
	}
	
	protected void onDraw(Canvas c) {
		c.drawPaint(paint);
		Long now = System.currentTimeMillis();
		Long timeleft = 265 - ((now - when) / 1000);
		if (when == (long)0 || timeleft <= (long)0) {			
			c.drawText("Flash " + id, 8, 20, textpaint);
			c.drawText("on", 8, 40, textpaint);
		} else {
			c.drawText("Flash " + id, 8, 20, textpaint);
			c.drawText("re in " + timeleft.toString(), 8, 40, textpaint);
		}
		invalidate();
	}
	

	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			setFlash();
			insession.sendMsg("say", "flash " + id);
		}
		return false;
	}
	
	public void setFlash() {
		when = System.currentTimeMillis();
	}

}
